//
//  DFLiveViewVC.m
//  DFMessage
//
//  Created by lanou on 15/10/27.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "DFLiveViewVC.h"
#import "DFLiveView.h"
#import "PKRequestManager.h"
#import "LiveModel.h"
#import "Header.pch"
#import "DFClickAppleViewController.h"
#import "MJRefresh.h"

@interface DFLiveViewVC ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *DataSourceArray;
@property(nonatomic,strong)LiveModel *model;
@property(nonatomic,assign)int page;
@end

@implementation DFLiveViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.tableView registerClass:[DFLiveView class] forCellReuseIdentifier:@"cell"];
    
      _DataSourceArray = [NSMutableArray array];

    [self requestDataWithType:0];
   
    
    self.tableView.rowHeight  = 177;
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];
    
}






-(void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=15&isReviewing=NO&last_time=2015-10-27%2019%3A24&page=%d&retina=1&vs=iph440",_page];
    [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        
      NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers |NSJSONReadingMutableLeaves error:nil];
        if (type == 0) {
            [self.DataSourceArray removeAllObjects];
        }
        NSArray *listArray = dic[@"list"];
        for (NSDictionary *modelDic in listArray) {
            _model = [[LiveModel alloc]init];
            
            [_model setValuesForKeysWithDictionary:modelDic];
            [self.DataSourceArray addObject:_model];
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
       [self.tableView reloadData];
        
        
    } error:^(NSError *error) {
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
    }];
    
    
    
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

      return _DataSourceArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    DFLiveView *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];

    cell.configureModel = _DataSourceArray[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LiveModel *model = _DataSourceArray[indexPath.row];
    DFClickAppleViewController *liveWeb = [[DFClickAppleViewController alloc] init];
    liveWeb.str = model.url;
    [self.navigationController pushViewController:liveWeb animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
